#include <bits/stdc++.h>

using namespace std;


long long prices[10000];

int main()
{
    int n = 0;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> prices[i];
    }

    int i = 0, j = 1;
    long long sum = 0;
    while(j < n)
    {
        if(prices[j] - prices[i] >= 0)
        {
            sum += prices[j] - prices[i];
            i = j;
            j++;
        }else{
            i++;
            j++;
        }   
    }

    cout << sum;
    return 0;
}